package mxm.amfire;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import me.yokeyword.fragmentation.SupportFragment;
import mxm.amfire.adapter.HomePagerFragmentAdapter;
import mxm.amfire.event.StartFragEvent;

/**
 * Created by zengpeijin on 2017/2/13.
 */

public class MainFragment extends SupportFragment implements ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private View rootView;
    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Subscribe
    public void startFragment(StartFragEvent event) {
        start(event.tagFragment);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_main,container,false);
        EventBus.getDefault().register(this);
        initView();
        return rootView;
    }

    private void initView() {
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
        viewPager.setAdapter(new HomePagerFragmentAdapter(getFragmentManager()));
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        viewPager.removeOnPageChangeListener(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0){
            rootView.findViewById(R.id.iv_left).setVisibility(View.GONE);
            rootView.findViewById(R.id.iv_right).setVisibility(View.VISIBLE);
        }else{
            rootView.findViewById(R.id.iv_left).setVisibility(View.VISIBLE);
            rootView.findViewById(R.id.iv_right).setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
    }
}
